package com.example.network_skd;

import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

public class SingletonRetrofit {
    private static SingletonRetrofit singletonRetrofit;
    private  Retrofit retrofit;
    private  OkHttpClient okHttpClient;

    private SingletonRetrofit() {
        OkHttpClient.Builder builder = new OkHttpClient.Builder();
        okHttpClient = builder.connectTimeout(30, TimeUnit.SECONDS)
                .readTimeout(30, TimeUnit.SECONDS)
                .writeTimeout(30, TimeUnit.SECONDS)
                .addInterceptor(new HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY))
                .build();
        retrofit = new Retrofit.Builder()
                .baseUrl(ApiConst.RELEASEBASEURL)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .client(okHttpClient)
                .build();

    }


    public static SingletonRetrofit getInstance(){

            if (singletonRetrofit ==null){
                synchronized (SingletonRetrofit.class){
                    if (singletonRetrofit==null)
                        singletonRetrofit = new SingletonRetrofit();
                }
            }
        return singletonRetrofit;
    }

    public Retrofit getRetrofit() {
        return retrofit;
    }

    public OkHttpClient getOkHttpClient() {
        return okHttpClient;
    }
}
